package com.lovestudying.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lovestudying.entity.Account;
import com.lovestudying.service.AccountService;
import com.lovestudying.service.impl.AccountServiceImpl;

/**
 * Servlet implementation class ManagerUserServlet
 */
@WebServlet("/ManagerUserServlet")
public class ManagerUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       AccountService accountService = new AccountServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManagerUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("getAccount".equals(op) || op == null) {
			getAccount(request,response);
		}else if ("deleteAccount".equals(op)) {
			deleteAccount(request,response);
		} else if ("updateState".equals(op)) {
			updateAccount(request,response);
		} else if ("getAccountByAccountName".equals(op)) {
			getAccountByAccountName(request,response);
		} 
		
		
		
	}

	private void getAccountByAccountName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountName = request.getParameter("accountName");
		List<Account> accountByaccountName = accountService.getAccountByaccountName(accountName);
		
		
		Gson gson = new Gson();
		String jsonObject = gson.toJson(accountByaccountName);
		
		response.setContentType("application/json");
		response.getWriter().write(jsonObject);
		
		
		
	}

	private void updateAccount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountId = request.getParameter("accountId");
		Integer accountState = Integer.parseInt(request.getParameter("accountState"));
		
		accountService.updateByAccountId(accountId, accountState);
		
		getAccount(request, response);
		
	}

	private void deleteAccount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountId = request.getParameter("accountId");
		accountService.deleteAccountByAccountId(accountId);
	   //System.out.println(accountId);
	     
		getAccount(request,response);
	}

	private void getAccount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Account> accountInfo = accountService.getAccountInfo();
		
		
		//System.out.println(accountInfo);
		Gson gson = new Gson();
		String jsonObject = gson.toJson(accountInfo);
		
		response.setContentType("application/json");
		response.getWriter().write(jsonObject);
				
	   
		
		
	}

}
